package com.example.backend.service.impl;


import com.example.backend.entity.User;
import com.example.backend.repository.UserRepository;
import com.example.backend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserRepository userRepository;

    @Override
    public boolean findByUsernameAndPassword(String username, String password) {
        if(userRepository.findByUsernameAndPassword(username,password)!=null)
            return true;
        return false;
    }

    @Override
    public void insert( User user) {
        userRepository.save(user);

    }

    @Override
    public boolean findByUsername(String username) {
        if(userRepository.findByUsername(username)!=null){
            return true;
        }
        return false;
    }


    @Override
    public User findUser(String username) {
        return userRepository.findByUsername(username);
    }

    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }

    @Override
    public List<User> findByPassword(String password) {
        return userRepository.findByPassword(password);
    }

    @Override
    public List<User> findById(Integer id) {
        if (!userRepository.findById(id).isPresent()){
            return null;
        }
        else {
            User u =userRepository.findById(id).get();
            return List.of(u);
        }
    }

    @Override
    public List<User> findByPermission(Integer permission) {
        return userRepository.findByPermission(permission);
    }

    @Override
    public void delete(Integer id) {
        User u = userRepository.findById(id).get();
        userRepository.delete(u);
    }
}
